
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maBL = ISS.SESSION.getSession('session_maBL');

    var comment = {};
    comment.maBL = _maBL;
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editProductComment',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(comment),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });
}

function loadDataComplete(comment){
    ISS.DOM.setVal('#AdProductComment_code', comment.maBL);
    ISS.DOM.setVal('#AdProductComment_product', comment.sanPham.maSP);
    ISS.DOM.setVal('#AdProductComment_name', comment.ten);
    ISS.DOM.setVal('#AdProductComment_email', comment.email);
    ISS.DOM.setVal('#AdProductComment_content', comment.noiDung);
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#AdProductComment_Modified', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var measurement = {};

    measurement.maBL = ISS.DOM.getVal('#AdProductComment_code');
    measurement.ten = ISS.DOM.getVal('#AdProductComment_name');
    measurement.email = ISS.DOM.getVal('#AdProductComment_email');
    measurement.noiDung = ISS.DOM.getVal('#AdProductComment_content');

    var product = {};
    product.maSP = ISS.DOM.getVal('#AdProductComment_product');
    measurement.sanPham = product;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerProductCommentEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(measurement),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(measurement) {
    if(measurement) {
        ISS.WINDOW.location('/jsp/admin/ad-product-comment-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}
